package no.ntnu.fp.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import no.ntnu.fp.net.Connection;

public abstract class AbstractModel implements Packable {
	public int id;

	public int getId() {
		return id;
	}

	public boolean isNew() {
		return id < 0;
	}

//	private boolean locked = false;
//
//	public void lock() {
//		locked = true;
//	}
//
//	public void unlock() {
//		locked = false;
//	}
//
//	public boolean isLocked() {
//		return locked;
//	}
	

	public void delete() {
		throw new org.apache.commons.lang.NotImplementedException();
	}

	public static List<AbstractModel> getAllNew() {
		List<AbstractModel> list = new ArrayList<AbstractModel>();
		for (Map.Entry<Integer, Person> entry : Person.instances.entrySet()) {
			Integer id = entry.getKey();
			AbstractModel model = entry.getValue();
			if (model.isNew()) {
				list.add(model);
				// Person.instances.remove(id);
			}
		}
		return list;
	}

	static protected Connection connection;

	protected abstract void writeAttribute(String attr, Object value);

	@Override
	public void unpack(Map<String, Object> data) {
		for (String k : data.keySet()) {
			writeAttribute(k, data.get(k));
		}
	}

	protected static int requestId() {
		return (int) (Math.random() * Integer.MIN_VALUE);
	}

	protected Map<String, Object> createAttributeMap() {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("id", id);
		return m;
	}
}
